package com.manolo.engine.entity;

import java.util.*;

import com.manolo.engine.util.Util;
import com.manolo.event.entity.Card;
import com.manolo.event.entity.SUIT;

/**
 * @authors Rafael de Souza - rafaeljec@gmail.com
 * 			Gustavo Renato Meyer
 * 			Tiago Gauziski
 * @date    15/03/2011
 */

public class Deck {
	
	public List<Card> deck;
	private int type;
	private int cardsPerHand;
	private int hands;
	
	public Deck(int typeOfDeck, int numberOfCardsPerHand, int numberOfHands) {
		deck = new ArrayList<Card>();
		type = typeOfDeck;
		setCardsPerHand(numberOfCardsPerHand);
		setHands(numberOfHands);
		createDeck();
	}
	
	public List<Card> getDeck() {
		return deck;
	}

	public void setDeck(List<Card> deck) {
		this.deck = deck;
	}

	public void setHands(int hands) {
		this.hands = hands;
	}

	public int getHands() {
		return hands;
	}

	public void setCardsPerHand(int cardsPerHand) {
		this.cardsPerHand = cardsPerHand;
	}

	public int getCardsPerHand() {
		return cardsPerHand;
	}
	
	/*
	 * The variable "typeOfDeck" below define how will be create the deck
	 * 1 - Spanish Deck (deck to play "truco", without nine and eight cards)
	 * 2 - Normal Deck (deck to play "canastra", "caixeta", and others)
	 */
		
	/*
	 *  Method to create a deck
	 */
	private void createDeck() {
		Integer cardNumber = 1, nipeNumber = 1, force;
		
		while(nipeNumber <= 4) {
			cardNumber = 1;
			while(cardNumber <= 12) {
				force = -3;
				if(cardNumber <= 3) 
					force = 7; 
				if(cardNumber >= 9)
					force = -5;
				if((this.type == 1) && ((cardNumber == 8) || (cardNumber == 9))) {
					cardNumber++;
				}
				else
				{
					Card newCard = new Card(cardNumber, Util.getSuit(nipeNumber), (cardNumber + force));
					deck.add(newCard);
					cardNumber++;
				}
			}
			nipeNumber++;
		}
	}	
	
	/*
	 *  Method to shuffle the deck
	 */
	public void shuffleDeck() {
		Collections.shuffle(this.deck);
	}
	
	/*
	 *  Method to cut the deck in the position choose by the player
	 */
	public void cutDeck(int cutPosition) {
		
		ArrayList<Card> newDeck = new ArrayList<Card>();
			
		while(cutPosition > 0) {
			newDeck.add(this.deck.get(0));
			this.deck.remove(0);
			cutPosition--;
		}
		this.deck.addAll(newDeck);
	}
	
	/*
	 *  Method to show all the cards of the deck
	 */
	public void showDeck() {
		for (Card card : this.deck) {
			System.out.println(card.getNumber() + "" + card.getSuit());
		}
		System.out.println("------------------------------------------------");
	}
	
	/*
	 *  Method to distribute the cards to four hands.
	 */
	public ArrayList<List<Card>> distributeCards() {
		ArrayList<List<Card>> allHands = new ArrayList<List<Card>>();
		Card handCard = new Card();
		int contHands = 0, contCards = 1, forceOfJoker = this.deck.get(this.cardsPerHand * this.hands).getForce();
		// TODO remove the line below
		System.out.println("Virou: " + this.deck.get(this.cardsPerHand * this.hands).getNumber());
		
		
		
		while(contHands <= (this.hands - 1)) {
			allHands.add(new ArrayList<Card>());
			contHands++;
		}
		
		while(contCards <= 12) {
			contHands = 0;
			while(contHands <= (this.hands - 1)) {
				// the code below set the new force is the four cards that are the jokers
				if(((forceOfJoker != 10) && (this.deck.get(0).getForce() == (forceOfJoker + 1))) || ((forceOfJoker == 10 ) && (this.deck.get(0).getForce() == 1))) {
					this.deck.get(0).setForce(this.setForceOfJoker(this.deck.get(0)));
				}
				handCard = this.deck.get(0);
				handCard.setStatus(1);
				allHands.get(contHands).add(handCard);
				this.deck.remove(0);
				contCards++;
				contHands++;
			}
		}
		return allHands;
	} 
	
	public int setForceOfJoker(Card card) {
		if(card.getSuit() == SUIT.CLUBS) {
			return 14;
		}
		else if(card.getSuit() == SUIT.HEARTS) { 
			return 13;
		}
		else if(card.getSuit() == SUIT.SPADES){
			return 12;
		}
		return 11;
	}
	
	
	
	
	public static void main(String[] args) {
		Deck deck = new Deck(1, 3, 4);
		//Shuffle the Deck
		deck.shuffleDeck();
		//Cut the deck at the position 2
		deck.cutDeck(22);
		
		ArrayList<List<Card>> allHands = deck.distributeCards();
		
		/*
		 * The code below show all four hand distributed 
		 * This was written to test the methods above, you can remove it that the class will work normally
		 */
		 
		System.out.println("----------------");
		int contHands = 0;
		while(contHands <= 3){
			System.out.println(allHands.get(contHands).get(0).getNumber() + "" + allHands.get(contHands).get(0).getSuit() + "" + allHands.get(contHands).get(0).getForce());
			System.out.println(allHands.get(contHands).get(1).getNumber() + "" + allHands.get(contHands).get(1).getSuit() + "" + allHands.get(contHands).get(1).getForce());
			System.out.println(allHands.get(contHands).get(2).getNumber() + "" + allHands.get(contHands).get(2).getSuit() + "" + allHands.get(contHands).get(2).getForce());
			System.out.println("----------------");
			contHands++;
		}
		System.out.println("Virou: " + deck.getDeck().get(0).getNumber() + "" + deck.getDeck().get(0).getSuit());
	}

	
}

